package main

import (
	"fmt"
	"io/fs"
	"os"
	"path/filepath"
	"regexp"
	"unicode/utf8"
)

func main() {
	// 定义正则表达式匹配包含中文或英文圆括号的文件名
	// \p{P} 匹配任何标点符号，这里我们使用 Unicode 范围来指定中文和英文圆括号
	re := regexp.MustCompile(`^([\p{P}]?\d+[\p{P}]?)\.(jpg|png)$`)

	err := filepath.Walk(".", func(path string, info fs.FileInfo, err error) error {
		if err != nil {
			fmt.Printf("Error accessing path %s: %v\n", path, err)
			return err
		}

		// 检查是否是文件
		if !info.IsDir() {
			fileName := filepath.Base(path)
			if matches := re.FindStringSubmatch(fileName); len(matches) > 0 {
				// 构造新的文件名，去掉括号
				newName := matches[1] + "." + matches[2]
				newPath := filepath.Join(filepath.Dir(path), newName)

				// 检查新文件名是否有效
				if _, err := utf8.DecodeRuneInString(newName); err == 0 {
					fmt.Printf("Invalid file name %s\n", newName)
					return nil
				}

				// 重命名文件
				if err := os.Rename(path, newPath); err != nil {
					fmt.Printf("Failed to rename %s to %s: %v\n", path, newPath, err)
				} else {
					fmt.Printf("Renamed %s to %s\n", path, newPath)
				}
			}
		}

		return nil
	})

	if err != nil {
		fmt.Printf("Error walking the path: %v\n", err)
	}
}
